package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.activity.PhoneCodeLoginActivity;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.fragment.MyScheduleFragment;
import com.mission.schedule.utils.MyViewPager;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 清单编辑右侧菜单对话框
 * Created by liwei on 2017/4/18
 */

public class CalenderNoteEditRightMenuDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;

    TextView image;


    public CalenderNoteEditRightMenuDialog(@NonNull final Context context, @StyleRes int themeResId,
                                           WindowManager windowManager, final boolean isimage) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        setCanceledOnTouchOutside(true);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_calender_new_detail, null);
        setContentView(view);
        ArrayList<View> viewList = new ArrayList<>();
        MyViewPager myViewPager = (MyViewPager) view.findViewById(R.id.viewpager_detail);

        View viewOne = LayoutInflater.from(context).inflate(R.layout.dialog_calender_noteedit_detail_item1, null);
        View viewTwo = LayoutInflater.from(context).inflate(R.layout.dialog_calender_noteedit_detail_item2, null);

        image = (TextView) viewOne.findViewById(R.id.addimage);


        if (isimage){
            image.setText("取消图片");
        }else {
            image.setText("插入图片");
        }

        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.image(isimage, 0);
                dismiss();
            }
        });

        image.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                callBack.image(isimage, 1);
                dismiss();
                return true;
            }
        });

        viewTwo.findViewById(R.id.weixin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
                    callBack.shareweixin();
                } else {
                    VipDailog();
                }
                dismiss();
            }
        });
        viewOne.findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.savenote();
                dismiss();
            }
        });

        viewOne.findViewById(R.id.share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.share();
                dismiss();
            }
        });

        viewOne.findViewById(R.id.copyall).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.copyone();
                dismiss();
            }
        });
        viewOne.findViewById(R.id.copyallstyle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callBack.copyall();
                dismiss();
            }
        });



        viewList.add(viewOne);
        viewList.add(viewTwo);

        ViewAdapter viewAdapter = new ViewAdapter(viewList);
        myViewPager.setAdapter(viewAdapter);

        final ImageView yd1 = (ImageView) view.findViewById(R.id.yd1);
        final ImageView yd2 = (ImageView) view.findViewById(R.id.yd2);

        myViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                yd1.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd1));
                yd2.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd1));
                if (position == 0) {
                    yd1.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd2));
                }
                if (position == 1) {
                    yd2.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_yd2));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    class ViewAdapter extends PagerAdapter {
        private List<View> viewList;//数据源

        public ViewAdapter(List<View> viewList) {
            this.viewList = viewList;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }
    }
    private void VipDailog() {
        final android.app.AlertDialog builder = new android.app.AlertDialog.Builder(context).create();

        builder.show();
        Window window = builder.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setAttributes(params);// 设置生效
        window.setContentView(R.layout.dialog_alter);
        TextView delete_ok = (TextView) window.findViewById(R.id.delete_ok);
        delete_ok.setText("注册");
        delete_ok.setTextColor(Color.RED);
        delete_ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent newt = new Intent(context,PhoneCodeLoginActivity.class);
                newt.putExtra("type", 1);
                newt.putExtra("login",true);
                context.startActivity(newt);
            }
        });
        TextView cancle_tv = (TextView) window.findViewById(R.id.cancle_tv);
        cancle_tv.setText("忽略");
        cancle_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
            }
        });
        TextView delete_tv = (TextView) window.findViewById(R.id.delete_tv);


        delete_tv.setText("该功能是正式用户功能\n请注册正式用户");

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth() - 30;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void image(boolean isimage, int type);
        void shareweixin();
        void savenote();
        void copyall();
        void copyone();
        void share();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

}
